package cn.getech.data.development.job.util

import java.io.{FileNotFoundException, FileOutputStream, IOException, InputStream, OutputStream}
import java.util.Properties

import org.slf4j.LoggerFactory

object PropertiesUtils {

  private val log = LoggerFactory.getLogger(this.getClass)

  def getProperties(path: String): Properties = {
    val prop = new Properties
    loadProp(prop, path)
    prop
  }

  private def loadProp(p: Properties, conf: String): Unit = {
    val is: InputStream = getInputStream(conf)
    if (null != is) try
      p.load(is)
    catch {
      case e: IOException =>
        log.info("file not found!")
    } finally if (is != null) try
      is.close()
    catch {
      case e: IOException =>
        log.info("stream close fail!")
    }
  }

  //获取输入流
  private def getInputStream(conf: String): InputStream = {
    val classLoader: ClassLoader = Thread.currentThread.getContextClassLoader
    var is: InputStream = null
    if (null != classLoader) is = classLoader.getResourceAsStream(conf)
    is
  }

  //获取输出流
  private def getOutPutStream(conf: String): OutputStream = {
    val classLoader: ClassLoader = Thread.currentThread.getContextClassLoader
    var out: OutputStream = null
    if (null != classLoader) {
      val filePath: String = classLoader.getResource(conf).getFile
      try
        out = new FileOutputStream(filePath)
      catch {
        case e: FileNotFoundException =>
          log.info("file not found!!!")
      }
    }
    out
  }

  //根据key读取value
  def getValue(p: Properties, key: String): String = {
    val value: String = p.getProperty(key)
    if (value == null) ""
    else value
  }

  //设置key=value
  def setValue(conf: String, key: String, value: String): Unit = {
    val p: Properties = getProperties(conf)
    val out: OutputStream = getOutPutStream(conf)
    p.setProperty(key, value)
    try
      p.store(out, "set:" + key + "=" + value)
    catch {
      case e: IOException =>
        log.info("set properties fail!!!")
    } finally if (out != null) try
      out.close()
    catch {
      case e: IOException =>
        log.info("stream close fail!")
    }
  }

  def main(args: Array[String]): Unit = {
    /*username="root"
    password="db@123"
    driver="com.mysql.jdbc.Driver"
    mysqlUrl="jdbc:mysql://bigdata-test-7:3306/bdp-admin"
    kafka_zookeeper_quorum="bigdata-test-4,bigdata-test-5,bigdata-test-6"
    hbase_zookeeper_quorum="bigdata-test-4,bigdata-test-5,bigdata-test-6"
    zookeeper_port="2181"
    outputTopic="vm_forecast_result"
    requestModelUrl="http://bigdata-test-7:5000/api/v1/vm/forecast"
    output_table="vm_forecast_result"*/
    val properties: Properties = getProperties("requestmodel.properties")
    val str: String = getValue(properties,"username")
    val str1: String = getValue(properties,"password")
    val str2: String = getValue(properties,"kafka_zookeeper_quorum")
    val str3: String = getValue(properties,"output_table")
    println(str)
    println(str1)
    println(str2)
    println(str3)
  }
}